package controller

import (
	"github.com/gin-gonic/gin"
	"hanyun-admin/src/entity"
	"hanyun-admin/src/service"
	"net/http"
	"strconv"
	"time"
)

//数据列表
func GetPostList(c *gin.Context)  {
	var post entity.Post
	c.Bind(&post)
	postList := service.SelectPostList(post)
	count := service.SelectPostListCount(post)
	c.JSON(200,gin.H{
		"code":200,
		"msg":0,
		"rows":postList,
		"total":count,
	})
}
//编辑
func GetPostInfoByPostId(c *gin.Context)  {
	postId, _ := strconv.ParseInt(c.Param("postId"),10,64)
	post := service.SelectPostInfoByPostId(postId)
	c.JSON(http.StatusOK,gin.H{
		"code":http.StatusOK,
		"msg":"操作成功！",
		"data":post,
	})
}
//添加
func AddSysPost(c *gin.Context)  {
	var post entity.Post
	c.BindJSON(&post)
	post.CreateTime = time.Now()
	err := service.InsertSysPost(post)
	if err != nil {
		c.JSON(http.StatusOK,gin.H{
			"code":http.StatusInternalServerError,
			"msg":"操作失败",
		})
		return
	}
	c.JSON(http.StatusOK,gin.H{
		"code":http.StatusOK,
		"msg":"操作成功！",
	})
}
//修改
func UpdateSysPostData(c *gin.Context)  {
	var post entity.Post
	c.BindJSON(&post)
	post.UpdateTime=time.Now()
	err := service.UpdateSysPostData(post)
	if err != nil {
		c.JSON(http.StatusOK,gin.H{
			"code":http.StatusInternalServerError,
			"msg":err,
		})
		return
	}
	c.JSON(http.StatusOK,gin.H{
		"code":http.StatusOK,
		"msg":"操作成功",
	})
}
//删除
func DeleteSysPost(c *gin.Context)  {
	postId, _:= strconv.ParseInt(c.Param("postId"),10,64)
	err := service.DeleteSysPostByPostId(postId)
	if err != nil {
		c.JSON(http.StatusOK,gin.H{
			"code":http.StatusInternalServerError,
			"msg":err,
		})
		return
	}
	c.JSON(http.StatusOK,gin.H{
		"code":http.StatusOK,
		"msg":"操作成功",
	})
}